/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.server;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.weissenborn.colabtex.server.tex.TexUtil;


/**
 * The Class DownloadPDF is a servlet, that downloads requested pdf file to the client
 */
public class DownloadPDF extends HttpServlet {

/* (non-Javadoc)
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
public void doGet(HttpServletRequest request, HttpServletResponse response) {
    Cookie[] cookies = request.getCookies();
    String path = "";
    if(cookies == null)
    	return;
    for (int i = 0; i < cookies.length; i++) {
		if(cookies[i].getName().equalsIgnoreCase("padId")) {
			String padId = cookies[i].getValue();
			path = TexUtil.getDirectory(padId)+"/"+padId.substring(padId.lastIndexOf("%24")+3)+".pdf";;
			path = path.replaceAll("%2F", "/");
			break;
		}
			
	}
    try {
        // get cookies, generate PDF.
        // If PDF is generated to to temp file, read it
        byte[] bytes = getFile(path);
        sendPDF(response, bytes, path);
    } catch (Exception ex) {  }
}
	
	/**
	 * Gets the file.
	 *
	 * @param filename the filename
	 * @return the file
	 */
	byte[] getFile(String filename) {
	
	    byte[] bytes = null;
	
	    try {
	        java.io.File file = new java.io.File(filename);
	        FileInputStream fis = new FileInputStream(file);
	        bytes = new byte[(int) file.length()];
	        fis.read(bytes);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	
	    return bytes;
	}
	
	/**
	 * Send pdf.
	 *
	 * @param response the response
	 * @param bytes the bytes
	 * @param name the name
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	void sendPDF(HttpServletResponse response, byte[] bytes, String name) throws IOException {
	    ServletOutputStream stream = null;
	
	    stream = response.getOutputStream();
	    response.setContentType("application/pdf");
	    response.addHeader("Content-Type", "application/pdf");
	    response.addHeader("Content-Disposition", "inline; filename=pad.pdf");
	    response.setContentLength((int) bytes.length);
	    stream.write(bytes);
	    stream.close();
	}
}
